package org.eenie.learn.seedquartz.error;

import org.eenie.learn.seedquartz.api.ApiResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.UnexpectedTypeException;

@RestControllerAdvice
public class ErrorControllerAdvice {

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Object errorHandler(Exception ex) {
        ex.printStackTrace();
        if (ex instanceof UnexpectedTypeException) {
            return ApiResponse.fail(501, "params do not match");
        }
        return ApiResponse.fail(500, "unknown error");
    }
}
